package com.ogawa.projectcommon.viewmodel

import androidx.lifecycle.*
import com.blankj.utilcode.util.LogUtils
import com.ogawa.base.base.BaseViewModel
import com.ogawa.base.bean.LoadState
import com.ogawa.base.bean.MqttAccountBean
import com.ogawa.projectcommon.utils.dataConvert
import com.ogawa.projectcommon.utils.launch
import com.ogawa.projectcommon.service.ProjectCommonApiService
import com.ogawa.projectcommon.network.RetrofitManager
import com.ogawa.base.utils.SpUtil


class GetMqttAccountViewModel : BaseViewModel() {
    val mqttAccountBean = MutableLiveData<MqttAccountBean>()

    fun getMqttAccount() {
        launch({
            val sn = SpUtil.get(SpUtil.SN, "") as String
            if (sn.isEmpty()) {
                LogUtils.e("sn为空:${sn}")
                return@launch
            }
            loadState.value = LoadState.Loading("")
            mqttAccountBean.value =
                RetrofitManager.INSTANCE.createService(ProjectCommonApiService::class.java)
                    .getMqttAccount(sn)
                    .dataConvert()

            loadState.value = LoadState.Success("")
        }
            , { loadState.value = LoadState.Fail(it.message ?: "") }
            , {})
    }

}
